//
//  HomeUploadTableViewCell.swift
//  MT
//
//  Created by M on 2019/12/29.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol HomeUploadTableViewCellDelegate: NSObjectProtocol {
    /// 点击关闭按钮
    func uploadTableViewCell(_ cell: HomeUploadTableViewCell, didClickDismissButtonWithModel model: HomeUploadModel)
}

class HomeUploadTableViewCell: UITableViewCell {
    
    /// 代理
    weak var delegate: HomeUploadTableViewCellDelegate?
    
    /// 上传模型
    var model: HomeUploadModel? {
        didSet {
            guard let model = model else { return }
            /// 进度条\
            
            progressLine.frame = CGRect(x: 0, y: 0, width: ScreenWidth * CGFloat(model.progress), height: 1)
            /// 封面图
            if let asset = model.assets?.first {
                requestPhoto(with: asset)
            }
            /// 文字
            msgLabel.text = model.msg
            /// 关闭按钮状态
            dismissButton.isHidden = model.status == .fail
            progressLine.isHidden = model.status == .fail
        }
    }
    
    /// 进度条
    lazy var progressLine: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.theme.main
        return view
    }()
    
    /// 封面图
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 4
        imgView.clipsToBounds = true
        return imgView
    }()
    
    /// 信息提示 Label
    lazy var msgLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(14)
        label.textColor = .white
        label.textAlignment = .left
        return label
    }()
    
    /// 关闭按钮
    lazy var dismissButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_dismiss"), for: .normal)
        btn.adjustsImageWhenHighlighted = false
        btn.isHidden = true
        btn.addTarget(self, action: #selector(uploadTableViewCell(didClickDismissButton:)), for: .touchUpInside)
        return btn
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension HomeUploadTableViewCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
    }
    
}


// MARK: ------------------ 初始化 ----------------------
extension HomeUploadTableViewCell {
    
    private func initializeUI() {
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = UIColor(hex: 0x161616).withAlphaComponent(0.5)
        contentView.addSubview(progressLine)
        contentView.addSubview(coverImageView)
        contentView.addSubview(msgLabel)
        contentView.addSubview(dismissButton)
        
        coverImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(contentView).offset(5)
            make.width.height.equalTo(30)
        }
        
        dismissButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(contentView)
            make.width.height.equalTo(26)
            make.right.equalTo(contentView).offset(-16)
        }
                
        msgLabel.snp.makeConstraints { (make) in
            make.left.equalTo(coverImageView.snp.right).offset(5)
            make.centerY.equalTo(contentView)
            make.right.equalTo(dismissButton.snp.left).offset(-10)
        }

    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
extension HomeUploadTableViewCell {
    
    private func uploadTableViewCell(didClickDismissButton sender: UIButton) {
        if let delegate = delegate, let model = model {
            delegate.uploadTableViewCell(self, didClickDismissButtonWithModel: model)
        }
    }
    
}

// MARK: ------------------ 图片读取 ----------------------
extension HomeUploadTableViewCell {
    
    private func requestPhoto(with asset: MTAsset?) {
        guard let asset = asset else { return }
        MTAssetLibrary.default.requestImage(withAsset: asset.asset, photoWidth: ScreenWidth / 4, progressHandler: nil, errorHandler: nil) { [weak self] (image, info, isDegraded) in
            guard let weakself = self else { return }
            /// 设置图片
            weakself.coverImageView.image = image
        }
    }
    
}
